package org.apache.ocean.merge;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;

import org.apache.ocean.ClusterUtil;
import org.apache.ocean.XMLUtil;
import org.jdom.Element;

/**
 *
 * @author jasonr
 */
public class MergeConfig {
	public static Logger log = Logger.getLogger(MergeConfig.class.getName());
  public List<URL> nameURls;
  public File nameURLsFile;
  public File nodeIDFile;
  public URL url;
  
  public MergeConfig(Element element) throws Exception {
    XMLUtil.reflectToObject(this, element);
  }
  
  public MergeConfig() {}
  
  public static MergeConfig loadMergeConfig() throws Exception {
    File mergeHome = ClusterUtil.getMergeHome();
    File configFile = new File(mergeHome, "config.xml");
    log.info("configFile: "+configFile.getAbsolutePath());
    Element element = XMLUtil.parseElement(configFile);
    MergeConfig mergeConfig = null;
    if (element != null) {
    	mergeConfig = (MergeConfig)ClusterUtil.construct(element, MergeConfig.class);
    } else mergeConfig = new MergeConfig();
    if (mergeConfig.url == null) throw new Exception("url not defined in config.xml");
    if (mergeConfig.nameURLsFile == null) mergeConfig.nameURLsFile = new File(mergeHome, "names.txt");
    if (mergeConfig.nodeIDFile == null) mergeConfig.nodeIDFile = new File(mergeHome, "nodeid.txt");
    if (!mergeConfig.nameURLsFile.exists()) throw new Exception(mergeConfig.nameURLsFile.getAbsolutePath()+" does not exist");
    return mergeConfig;
  }
  
  public static File getConfDirectory(File homeDirectory) {
    return new File(homeDirectory, "mergeconf");
  }
}
